package kassiopeia.agent.alerting;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataSource;

public class Alert {

    private final AlertType type;
    private final String content;
    private final String threadName;
    private final String[] attachments;

    public Alert(AlertType type, String content, String ... attachments) {
        this.type = type;
        this.content = content;
        this.attachments = attachments;
        this.threadName = Thread.currentThread().getName();
    }

    public String getSubject() {
        return type.getSubject();
    }

    public String getContent() {
        return content;
    }

    public boolean isHi() {
        return type.isHigh();
    }

    public String getThreadName() {
        return threadName;
    }

    public AlertType getType() {
        return this.type;
    }

    public DataSource[] getAttachments(final String name) {
        if (attachments==null || attachments.length==0) {
            return new DataSource[0];
        }
        DataSource[] ds = new DataSource[attachments.length];
        int i = 0;
        for (final String att: attachments) {
            final int n = i + 1;
            ds[i++] = new DataSource() {

                @Override
                public String getContentType() {
                    return "text/plain; charset=utf-8";
                }

                @Override
                public InputStream getInputStream() throws IOException {
                    return new ByteArrayInputStream(att.getBytes("UTF-8"));
                }

                @Override
                public String getName() {
                    return name + "-" + n + ".txt";
                }

                @Override
                public OutputStream getOutputStream() throws IOException {
                    return null;
                }
            };
        }
        return ds;
    }

}
